import Vue from "vue";
import VueRouter from "vue-router";
import Dvfullscreen from "../views/dvfullscreen.vue";
import Login from "../views/login/index.vue";
import { routerCenter } from "./routerCenter";
import store from "../store/index";
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.use(VueRouter);

const routes = [
  { path: "/login", component: Login },
  { path: "/", redirect: "/dvfullscreen/stationlocation" },
  {
    path: "/dvfullscreen",
    component: Dvfullscreen,
    redirect: "/dvfullscreen/stationlocation",
    children: routerCenter
  },
  {
    path: "*",
    redirect: "/404",
    meta: {}
  }
];
const router = new VueRouter({
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  if (to.path === "/404") return next();
  const tokenStr = sessionStorage.getItem("token_12");
  var userId = to.query.id;
  if (!tokenStr) {
    if (to.path === "/dvfullscreen/stationlocation" && userId) {
      store.dispatch("handleLogin", { userName: userId, passWord: "123456" });
      store.commit("SET_ISLINK", true);
      return next();
    } else {
      return next("/login");
    }
  } else if (tokenStr && userId) {
    if (to.path === "/dvfullscreen/stationlocation" && userId) {
      store.dispatch("handleLogin", { userName: userId, passWord: "123456" });
      store.commit("SET_ISLINK", true);
      return next();
    } else {
      return next("/login");
    }
  }
  next();
});
export default router;
